package com.jie.controller.admin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.pojo.Blog;
import com.jie.pojo.Comment;
import com.jie.service.PingLunService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 曾豪杰
 * @Date: 2022/09/17/18:48
 * @Description:
 */
@Controller
@RequestMapping("/admin")
public class PingLunController {
    @Autowired
    private PingLunService pingLunService;

    @GetMapping("/pinglun")
    public String pingLun(Comment comment, @RequestParam(defaultValue = "1",value = "page") int page,
                          @RequestParam(defaultValue = "8",value = "pageSize") Integer pageSize,
                          Model model){
        if(comment.getContent()==null|| Objects.equals(comment.getContent(), "")){
            comment.setContent(null);
        }
        if(comment.getNickName()==null|| Objects.equals(comment.getNickName(), "")){
            comment.setNickName(null);
        }
        //构造分页
        Page<Comment> pageInfo =new Page<>(page,pageSize);
        LambdaQueryWrapper<Comment> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(comment.getContent()!=null,Comment::getContent,comment.getContent());
        queryWrapper.like(comment.getNickName()!=null,Comment::getNickName,comment.getNickName());
        queryWrapper.orderByAsc(Comment::getCreateTime);
        Page<Comment> page1 = pingLunService.page(pageInfo, queryWrapper);
        model.addAttribute("page",page1);
        return "admin/pingLun";
    }

    @GetMapping("/delete/inputs")
    public String delete(Long id, RedirectAttributes redirectAttributes){
        LambdaQueryWrapper<Comment> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getId,id).or().eq(Comment::getParentCommentId,id);
        pingLunService.remove(queryWrapper);
        redirectAttributes.addFlashAttribute("message","删除成功！");
        return "redirect:/admin/pinglun";
    }

}
